package com.qyhl.school.school.center;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.flyco.tablayout.SlidingTabLayout;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.school.R;
import com.qyhl.school.R2;
import com.qyhl.school.school.SchoolHomeActivity;
import com.qyhl.school.school.center.activity.SchoolCenterActFragment;
import com.qyhl.school.school.center.vlog.SchoolCenterVlogFragment;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.school.SchoolCenterBean;
import com.qyhl.webtv.commonlib.entity.school.SchoolCertificateBean;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.commonlib.utils.eventbus.Event;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SchoolCenterFragment extends BaseFragment implements SchoolCenterContract.SchoolCenterView {
    @BindView(R2.id.reporter_tag)
    TextView reporterTag;
    @BindView(R2.id.nickName)
    TextView nickName;
    @BindView(R2.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R2.id.head_icon)
    RoundedImageView headIcon;
    @BindView(R2.id.like_num)
    TextView likeNum;
    @BindView(R2.id.work_num)
    TextView workNum;
    @BindView(R2.id.tabLayout)
    SlidingTabLayout tabLayout;
    @BindView(R2.id.view_pager)
    ViewPager viewPager;

    private SchoolCenterPresenter mPresenter;

    private String username;

    private SchoolCenterBean mBean;


    public static SchoolCenterFragment newInstance() {
        SchoolCenterFragment fragment = new SchoolCenterFragment();
        return fragment;
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_school_center, null);
    }

    @Override
    protected void create() {
        BusFactory.getBus().register(this);
        mPresenter = new SchoolCenterPresenter(this);

        username = "";
        likeNum.setText("0次点赞");
        workNum.setText("0个作品");
        nickName.setText("点击登录");
        headIcon.setImageResource(R.drawable.comment_head_default);

        List<String> nameList = new ArrayList<>();
        List<Fragment> fragments = new ArrayList<>();
        nameList.add("校园Vlog");
        nameList.add("我的活动");

        fragments.add(SchoolCenterVlogFragment.newInstance());
        fragments.add(SchoolCenterActFragment.newInstance());

        viewPager.setAdapter(new Adapter(getChildFragmentManager(), fragments, nameList));
        viewPager.setOffscreenPageLimit(fragments.size());
        tabLayout.setViewPager(viewPager);

        username = CommonUtils.getInstance().getUserName();
        if (StringUtils.isEmpty(username)) {
            likeNum.setVisibility(View.GONE);
            workNum.setVisibility(View.GONE);
            nickName.setText("点击登录");
            headIcon.setImageResource(R.drawable.comment_head_default);
        } else {
            mPresenter.getData(username);
        }
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {
        if (StringUtils.isNotEmpty(username)) {
            mPresenter.getData(username);
        }
    }

    @Override
    protected void onHide() {

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void setData(SchoolCenterBean bean) {
        dismissLoading();
        likeNum.setVisibility(View.VISIBLE);
        workNum.setVisibility(View.VISIBLE);
        mBean = bean;
        nickName.setText(bean.getUser().getNickName());
        Glide.with(SchoolCenterFragment.this)
                .load(bean.getUser().getLogo())
                .apply(new RequestOptions().error(R.drawable.comment_head_default).placeholder(R.drawable.comment_head_default))
                .into(headIcon);

        likeNum.setText(bean.getLikeCount() + "次点赞");

        workNum.setText(bean.getVlogCount() + "个作品");

    }

    @Override
    public void setCertificate(SchoolCertificateBean bean) {
        ARouter.getInstance()
                .build(ARouterPathConstant.SCHOOL_CENTER_CERTIFICATE)
                .withSerializable("bean",bean)
                .navigation();
    }

    @Override
    public void setError(String msg) {
        dismissLoading();
        mBean = null;
        showToast(msg, 4);
        nickName.setText("点击重试");
    }

    @Override
    public void setCertificateError(String msg) {
        showToast(msg);
    }

    @OnClick({R2.id.reporter_tag, R2.id.nickName, R2.id.head_icon, R2.id.back_btn})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.reporter_tag) {
            if (StringUtils.isEmpty(username)) {
                RouterManager.routerLogin(getActivity(), 0);
            } else {
                mPresenter.getCertificate(username);
            }
        } else if (id == R.id.nickName || id == R.id.head_icon) {
            if (StringUtils.isEmpty(username)) {
                RouterManager.routerLogin(getActivity(), 0);
            } else if (mBean == null) {
                mPresenter.getData(username);
                showLoading("获取中...");
            }
        } else if (id == R.id.back_btn) {
            if (getActivity() instanceof SchoolHomeActivity) {
                ((SchoolHomeActivity) getActivity()).doFinish();
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMessage(Event.LoginMessage event) {
        if (event != null) {
            if (event.isLogin()) {
                username = CommonUtils.getInstance().getUserName();
                mPresenter.getData(username);
            } else {
                username = "";
                likeNum.setVisibility(View.GONE);
                workNum.setVisibility(View.GONE);
                nickName.setText("点击登录");
                headIcon.setImageResource(R.drawable.comment_head_default);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BusFactory.getBus().unregister(this);
    }


    static class Adapter extends FragmentPagerAdapter {
        private List<Fragment> fragmentList;
        private List<String> names;

        Adapter(FragmentManager fm, List<Fragment> fragmentList, List<String> names) {
            super(fm);
            this.fragmentList = fragmentList;
            this.names = names;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return names.get(position);
        }
    }
}
